<template>
	<div class="export-excel">
		<el-button 
		:loading="downloadLoading" 
		style="margin:0 0 20px 0px;" 
		type="primary" 
		icon="el-icon-download" 
		size="small"
		@click="handleDownload">
			导出 Excel
		</el-button>
	</div>
</template>

<script>
	import _ from 'lodash'
	export default {
		name: 'export-excel',
		inheritAttrs: false,
		props: {
			head: {
				type: [String, Array],
				default: ''
			},
			keys: {
				type: Array,
				default: () => []
			},
			data: {
				type: Array,
				default: () => []
			}
		},
		data() {
			return {
				downloadLoading: false,
			};
		},
		methods: {
			handleDownload() {
				this.downloadLoading = true
				import('./Export2Excel').then(excel => {
					const header = Array.isArray(this.head) ? this.head : this.head.split(',');
					const data = _.map(this.data, o => _.values(_.pick(o, this.keys)));
					excel.export_json_to_excel({
						header,
						data,
						filename: `ecgap365_excel_${Date.now()}`,
					});
					this.downloadLoading = false;
				})
			}
		}
	};
</script>
